// ###############
// FILE : ActionFactory.java
// WRITER : Ido Adler, idoa03, 039809181
// WRITER : Gal Bartfeld, gbartf99, 20144546
// EXERCISE : oop herobots 2009
// DESCRIPTION:
// TODO: add description
// ###############
package warbots.entities.actions;


import java.awt.Point;

import warbots.entities.units.Unit;
import warbots.gameplay.Map;


/**
 * TODO: add description
 * 
 * @author Ido Adler.
 * @author Gal Bartfeld.
 */
public class ActionFactory
{
    public static Action newInstance(final Unit user, final String name)
    {
        String[] args = name.split(":");
        
        if (args[0].equals("HIT"))
        {
            if (args.length > 1)
                return new HitAction(user, "Hit:" + args[1]);
            else
                return new HitAction(user, "Hit:Basic");
        }
        
        if (args[0].equals("SHOOT"))
        {
            if (args.length > 2)
                return new ShootAction(user, "Shoot:" + args[1], Integer.parseInt(args[2]));
            else if(args.length > 1)
                return new ShootAction(user, "Shoot:Basic", Integer.parseInt(args[1]));
            else
                return new ShootAction(user, "Shoot:Basic");
        }
        
        if (args[0].equals("MOVE"))
        {
            if (args.length > 1)
                return new MoveAction(user, "Move:" + args[1]);
            else
                return new MoveAction(user, "Move:Basic");
        }
        
        if (args[0].equals("MHIT"))
        {
            if (args.length > 1)
                return new MoveAndHitAction(user, "MoveAndHit:" + args[1]);
            else
                return new MoveAndHitAction(user, "MoveAndHit:Basic");
        }
        
        if (args[0].equals("HEAL"))
        {
            if (args.length > 2)
                return new HealAction(user, "Heal:" + args[1], Integer.parseInt(args[2]));
            else if(args.length > 1)
            	return new HealAction(user, "Heal:Basic", Integer.parseInt(args[1]));
            else 
            	return new HealAction(user, "Heal:Basic");
        }
        
        return new Action(user, name)
        {
            @Override
            public boolean execute(final Map world, final Point target)
            {
                String name;
                if (world._board[target.x][target.y].getUnit() != null)
                    name = world._board[target.x][target.y].getUnit().getName();
                else
                    name = "[" + target.x + "," + target.y + "]";
                System.out.println(_parent.getName() + ":" + getName() + "->" + name);
                return true;
            }
        };
    }
}
